'use strict';

angular.module('app')
    .controller('updateFaceController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            var defaultAva = $rootScope.defaultAvatar;
            $scope.myImage='';
            // $scope.myCroppedImage=$scope.myCroppedImage ;
            $scope.myCroppedImage = '';
           if($state.includes('**.userfc.updateFace')){
                title="编辑头像";
               var id = $state.params.id;
               activate(id);
                $scope.record = {'Typeuser' :1};
                setTimeout(function(){
                    $scope.myCroppedImage = defaultAva;
                    !$rootScope.$$phase && $scope.$apply();
                },300);

            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            //初始化验证
            //validate($scope);
            $scope.uploaded= function(){
                $scope.loading = true;
                if($scope.myImage != '' && $scope.myImage != null && $scope.myImage != 'undefined') {
                    $.ajax({
                        url : $scope.app.project_name + '/upload/imageData',
                        data: {fileData:$scope.myCroppedImage}
                    }).then(function(result){
                        if(result && result.httpCode ==200){//成功
                            $scope.record['avatar'] =result.imgName[0];
                            saveData();
                        }/*else if(result && result.httpCode ==400){
                         saveData();
                         }*/
                    });
                } else {
                    $scope.record['avatar'] = $scope.myCroppedImage;
                    saveData();
                }
            };

            function saveData(){
                var m = $scope.record;
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.id ? $scope.app.project_name+'/user/updateFace' : $scope.app.project_name+'/user/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    console.log(result);
                    if(result.httpCode ==200){
                        if(result.data<0){
                            $scope.alertModal({"content":"账户名已存在"});
                            $scope.$apply(function(){
                                $scope.isDisabled = false;
                            })

                            //成功
                        }else{
                            $scope.alertModal({"content":"头像修改成功"});
                            $state.go('main.tpl.homePage');
                            $timeout(function(){
                                location.reload();
                            },2000);
                        }

                    }else{
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }

            var handleFileSelect=function(evt) {
                var file=evt.currentTarget.files[0];
                if(!/image\/\w+/.test(file.type)){
                    return false;
                }
                var reader = new FileReader();
                reader.onload = function (evt) {
                    $scope.$apply(function($scope){
                        $scope.myImage=evt.target.result;
                    });
                };
                reader.readAsDataURL(file);
            };
            angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
            $('.upload-pic-box a').click(function() {
                $('#fileInput').click();
            });

            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/user/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $timeout(function(){
                            $scope.myCroppedImage=$scope.record.avatar || defaultAva;//初始化 预览图
                            $scope.myImage = $scope.record.avatar || defaultAva;
                            $scope.record.avatar = null;
                        },300);
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.restImg = function () {
                var id = $state.params.id;
                activate(id);
            }


        }]);